package cn.didi.feidi.androidorder.adapter;

import android.content.Context;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;

import cn.didi.feidi.androidorder.R;
import cn.didi.feidi.androidorder.model.UserAddress;

/**
 * Created by 独步清风 on 2017/7/23.
 */

public class PersonReceiverAddressListViewAdapter extends ArrayAdapter<UserAddress> {

    private
    @LayoutRes
    int resourceId;

//    private List<UserAddress> mUserAddresses;


    private boolean showManager;
    private Callback mCallback;

    public PersonReceiverAddressListViewAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<UserAddress> objects) {
        super(context, resource, objects);
//        mUserAddresses = objects;
        resourceId = resource;
    }

    public void setCallback(Callback callback) {
        mCallback = callback;
    }

    public boolean isShowManager() {
        return showManager;
    }

    public void setShowManager(boolean showManager) {
        this.showManager = showManager;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        final UserAddress receiveMessage = getItem(position);

        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder = new ViewHolder();
            viewHolder.personName = (TextView) view.findViewById(R.id.person_receive_person_name);
            viewHolder.personPhone = (TextView) view.findViewById(R.id.person_receive_person_phone);
            viewHolder.personReceiveAddress = (TextView) view.findViewById(R.id.person_receive_person_address);
            viewHolder.defaultFlag = (TextView) view.findViewById(R.id.person_receive_default_address_flag);
            viewHolder.sex = (TextView) view.findViewById(R.id.person_receive_person_sex);
            viewHolder.llAddressManager = (LinearLayout) view.findViewById(R.id.personal_add_address_manager_parent_ll);
//            viewHolder.tvAddressDefault = (TextView) view.findViewById(R.id.personal_add_address_set_default_tv);
            viewHolder.tvAddressDelete = (TextView) view.findViewById(R.id.personal_add_address_delete_address_tv);
            viewHolder.tvAddressEdit = (TextView) view.findViewById(R.id.personal_add_address_edit_tv);
            view.setTag(viewHolder); // 将ViewHolder存储在View中
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag(); // 重新获取ViewHolder
        }
        viewHolder.personName.setText(receiveMessage.getName());
        viewHolder.personPhone.setText(receiveMessage.getPhoneNumber());
        viewHolder.personReceiveAddress.setText(receiveMessage.getCity() + receiveMessage.getDistrict()
                + receiveMessage.getRoad() + receiveMessage.getAddress());

        viewHolder.sex.setText(receiveMessage.getSex());

        if (receiveMessage.getDefaultAddress()) {
            viewHolder.defaultFlag.setVisibility(View.VISIBLE);
        } else {
            viewHolder.defaultFlag.setVisibility(View.INVISIBLE);
        }
        if (showManager) {
            viewHolder.llAddressManager.setVisibility(View.VISIBLE);
        } else {
            viewHolder.llAddressManager.setVisibility(View.GONE);
        }

        if (mCallback != null) {
            viewHolder.tvAddressDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mCallback.deleteCallback(receiveMessage);
                }
            });

            viewHolder.tvAddressEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mCallback.editCallback(receiveMessage);
                }
            });
        } else {
            viewHolder.tvAddressDelete.setOnClickListener(null);
            viewHolder.tvAddressEdit.setOnClickListener(null);
        }


        return view;

    }

    @Override
    public void insert(@Nullable UserAddress object, int index) {
        super.insert(object, index);
    }

    public interface Callback {

        void deleteCallback(UserAddress userAddress);

        void editCallback(UserAddress userAddress);

    }

    class ViewHolder {
        TextView personName;
        TextView personPhone;
        TextView personReceiveAddress;
        TextView defaultFlag;
        TextView sex;
        LinearLayout llAddressManager;
        //        TextView tvAddressDefault;
        TextView tvAddressDelete;
        TextView tvAddressEdit;
    }
}
